package com.autumn.scheduler.job.trigger;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Trigger;

import com.autumn.scheduler.domain.entities.AutumnJobDetails;
import com.autumn.scheduler.domain.values.SchedulerValue;
import com.autumn.util.ExceptionUtils;

/**
 * 
 * Cron 任务触发器
 * 
 * @author 杨昌国 2018-03-20 14:18:15
 */
public class CronJobTrigger extends AbstractJobTrigger {

	private final String cronExpression;

	/**
	 * 
	 * @param cronExpression
	 */
	public CronJobTrigger(String cronExpression) {
		this.cronExpression = ExceptionUtils.checkNotNullOrBlank(cronExpression, "cronExpression");
	}

	/**
	 * 获取表达式
	 * 
	 * @return
	 */
	public String getCronExpression() {
		return this.cronExpression;
	}

	@Override
	public Trigger createTrigger(JobDetail jobDetail, AutumnJobDetails autumnJobDetail, SchedulerValue schedulerValue) {
		autumnJobDetail.setCronExpression(this.getCronExpression());
		autumnJobDetail.setTimeInterval(null);
		autumnJobDetail.setTimeIntervalUnit("");		
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(this.getCronExpression());
		return this.createTrigger(jobDetail, autumnJobDetail, schedulerValue, scheduleBuilder);
	}

}
